using System;
using SecuBat.Common;
using SecuBat.Threading;

namespace SecuBat.Work
{
	/// <summary>
	/// Summary description for Task.
	/// </summary>
	public abstract class Task : SharedData, IDisposable
	{
		private TaskStatus _status;
		private String _taskActivity;
        private WorkerThread _runningInThread;
        private String _url;

		public TaskStatus Status
		{
			get 
            { 
                return LockedCopy(ref _status); 
            }
			set 
            { 
                LockedSet(ref _status, ref value);
            }
		}

		public string TaskActivity
		{
			get
			{
				return LockedCopy(ref _taskActivity);
			}
			set
			{
				LockedSet(ref _taskActivity, ref value);
            }
		}
        
        public string Url
        {
            get
            {
                return LockedCopy(ref _url);
            }
            set
            {
                LockedSet(ref _url, ref value);
            }
        }

		public Task()
		{
			_status = TaskStatus.None;
			TaskActivity = "";
		}

        public void SetRunningInWorkerThread(WorkerThread thread)
        {
            _runningInThread = thread;
        }

        protected void SignalizeStatusChange()
        {
            if (_runningInThread != null)
                _runningInThread.SignalizeStatusChange();
        }

		protected TaskStatus LockedCopy( ref TaskStatus status ) 
		{
			TaskStatus val;
			ReadLock();
			val = status;
			ReadRelease();
			return val;
		}

		protected void LockedSet( ref TaskStatus var, ref TaskStatus val ) 
		{
			WriteLock();
			var = val;
			WriteRelease();
			return;
		}

		public virtual void Initialize()
		{
			
		}

		public virtual void Run()
		{
			
		}

		public virtual void Dispose()
		{
			
		}
	}		
}
